<?php

/**
 * file actions.
 *
 * @package    jiazhang
 * @subpackage file
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fileActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  public function executeUpload(sfWebRequest $request){
       sfConfig::set('sf_web_debug',false);
      if($request->isMethod('POST')){
//          $is_cover = (int)$request->getParameter('is_cover',0);
          $file = $request->getFiles('imgFile');
          $file_name = $file['name'];
          $file_ext_tmp = explode('.', $file_name);
          $file_ext = $file_ext_tmp[count($file_ext_tmp)-1];
          $allow_file_type = array('jpg','bmp','png','gif','jpeg');
          
          if(in_array($file_ext, $allow_file_type)){
             $year = date('Y',time());
             $month = date('m',time());
             $day = date('d',time());
             $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.'.$file_ext;
             $upyun_save_path = '/article/'.$year.'/'.$month.'/'.$day.'/'.$save_file_name;
             $upyun = new UpYun(sfConfig::get('app_upyun_space'),sfConfig::get('app_upyun_user') , sfConfig::get('app_upyun_pwd'));
             
            try {
                $fh = fopen($file['tmp_name'], 'rb');
                $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
                fclose($fh);
            }
            catch(Exception $e) {
                echo $e->getCode();
                echo $e->getMessage();
            }
            
            $image_url=  sfConfig::get('app_img_url').$upyun_save_path;
            $json = new Services_JSON();
            $return_data = array('error' => 0, 'url' => $image_url);
            return $this->renderText($json->encode($return_data));
            exit();
          }else{
              
            if($is_cover){
                $return_data = array('errorCode' => 404,'errorMsg'=> '文件类型错误！');
            }else{
                $return_data = array('error' => 1, 'url' => '文件类型错误！');
            }
              //error 文件类型错误
              $json = new Services_JSON();
              return $this->renderText($json->encode($return_data));
              exit();
          }
          
      }
  }
  
}
